package ip.ejb.session.facade;
/***********************************************************************
 * Module:  CategoryFacadeBean.java
 * Author:  mojsilo
 * Purpose: Defines the Class CategoryFacadeBean
 ***********************************************************************/

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import ip.dto.CategoryDTO;
import ip.dto.UserDTO;
import ip.ejb.entity.CategoryLocal;
import ip.ejb.entity.CategoryLocalHome;
import ip.ejb.entity.CategoryPK;
import ip.ejb.entity.UserLocal;
import ip.ejb.entity.UserLocalHome;
import ip.ejb.entity.UserPK;
import ip.exceptions.CanNotCreateNewCategoryException;
import ip.exceptions.CanNotCreateNewUserException;
import ip.exceptions.NoSuchCategoryException;
import ip.exceptions.NoSuchUserException;
import ip.util.EJBHomeFactory;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;
/**
 * @ejb.bean
 *   name="CategoryFacade"
 *   type="Stateless"
 *   jndi-name="ip/ejb/facade/CategoryFacade"
 *   local-jndi-name="ip/ejb/facade/CategoryFacadeLocal"
 * @ejb.transaction type="Supports"
 */
public class CategoryFacadeBean implements SessionBean
{

	private static final long serialVersionUID = 1L;
	private SessionContext ctx;
	private CategoryLocalHome categoryLocalHome = null;
	private CategoryLocal categoryLocal = null;	
	private CategoryPK categoryPK = null;
	Logger logger = Logger.getLogger(this.getClass());

	/**
	 * @throws CanNotCreateNewCategoryException 
	 * @ejb.interface-method view-type="local"
	 */
	public void createCategory(CategoryDTO categoryDTO) throws CanNotCreateNewCategoryException{
		try {
			categoryLocalHome = (CategoryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CategoryLocalHome.class);
			categoryLocalHome.create(categoryDTO.getName()); 

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (CreateException e) {			
			e.printStackTrace();
			throw new CanNotCreateNewCategoryException(categoryDTO);
		}

	}

	/**
	 * @throws NoSuchCategoryException 
	 * @ejb.interface-method view-type="local"
	 */
	public void updateCategory(CategoryDTO categoryDTO) throws NoSuchCategoryException{
		try {
			categoryPK = new CategoryPK();
			categoryPK.setCategoryId(categoryDTO.getCategoryId());		
			categoryLocalHome = (CategoryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CategoryLocalHome.class);
			categoryLocal = categoryLocalHome.findByPrimaryKey(categoryPK);

			categoryLocal.setName(categoryDTO.getName());

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
			throw new NoSuchCategoryException(categoryDTO);
		}
	}

	/**
	 * @throws NoSuchCategoryException 
	 * @ejb.interface-method view-type="local"
	 */
	public CategoryDTO getCategory(CategoryDTO categoryDTO) throws NoSuchCategoryException{
		try {
			//there is possibility that there is no needed for this method
//			CategoryLocalHome = (CategoryLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/CategoryLocal");
			categoryLocalHome = (CategoryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CategoryLocalHome.class);
			categoryPK = new CategoryPK();
			categoryPK.setCategoryId(categoryDTO.getCategoryId());
			categoryLocal = categoryLocalHome.findByPrimaryKey(categoryPK);
			categoryDTO = createCategoryDTO(categoryLocal);

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
			throw new NoSuchCategoryException(categoryDTO);
		}
		return categoryDTO;

	}

	/**
	 * 
	 * @ejb.interface-method view-type="local"
	 */
	public Collection getAllCategories(){
		Collection categoriesDTO = null;
		try {
//			categoryLocalHome = (CategoryLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/CategoryLocal");
			categoryLocalHome = (CategoryLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.CategoryLocalHome.class);
			Collection categories = categoryLocalHome.findAll();

			if(categories.size()>0){
				categoriesDTO = new ArrayList();
				Iterator iter = categories.iterator();

				while (iter.hasNext()) {
					categoryLocal = (CategoryLocal) iter.next();
					categoriesDTO.add(createCategoryDTO(categoryLocal));
				}
			}

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();			
		}
		return categoriesDTO;
	}

	/**
	 * 
	 * @ejb.interface-method view-type="local"
	 */
	public CategoryDTO createCategoryDTO(CategoryLocal categoryLocal) {			
		CategoryDTO categoryDTO = null;
		if(categoryLocal != null){
			categoryDTO = new CategoryDTO();
			categoryDTO.setCategoryId(categoryLocal.getCategoryId());
			categoryDTO.setName(categoryLocal.getName());
		}
		return categoryDTO;
	}

	/**
	 * @ejb.create-method
	 */
	public void ejbCreate(){}
	public void ejbActivate(){}
	public void ejbPassivate(){}
	public void ejbRemove(){}
	public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException {
		this.ctx = ctx;
	}

}